package com.mesnac.comm.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SessionFilter implements Filter{
	
	private boolean sessionenable;

	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		//如果需要进行session过期的判定
		//System.out.println("==session filter worked");
		if(sessionenable){
			HttpServletRequest re = (HttpServletRequest)request;
			//System.out.println(re.getRequestURI());
			//System.out.println(re.getContextPath());
			//System.out.println(re.getRequestURL());
			//System.out.println(re.getSession().getAttribute("user"));
			HttpServletResponse resp =(HttpServletResponse)response;
			chain.doFilter(request, response);
			/*
			if(re.getSession().getAttribute("user")!=null||re.getRequestURI().endsWith("login.jsp")
					||re.getRequestURI().endsWith("index.jsp")
					||re.getRequestURI().endsWith(".css")||re.getRequestURI().endsWith(".jpg")
					||re.getRequestURI().endsWith(".gif")||re.getRequestURI().endsWith("usersLogin.action")
					||re.getRequestURI().endsWith("usersRegister.action")||re.getRequestURI().endsWith("election/")){
				
				chain.doFilter(request, response);
				return;
			}
			else {
				//resp.getWriter().write("<script language='javascript'>alert('time out')</script>");
				//resp.sendRedirect("http://127.0.0.1:8080/election/");
				re.getRequestDispatcher("../jspuser/sessionTimeout.jsp").forward(request, response);
			}
			*/
		}
		
		
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
		this.sessionenable = Boolean.valueOf(filterConfig.getInitParameter("sessionenable"));
	}

}
